﻿using Infrastructure.Dao;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WsdShop.Model.ViewModels;

namespace WsdShop.Service
{
    public class BaseService
    {
        public static SqlSugarClient _db;

        /// <summary>
        /// 取出单个实体
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public static T LoadEntity<T>(Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                return _db.Queryable<T>().FirstOrDefault(whereLambda);
            }
        }
        /// <summary>
        /// 取出符合条件的所有实体
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public static List<T> LoadEntities<T>(Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                return _db.Queryable<T>().Where(whereLambda).ToList();
            }
        }
        /// <summary>
        /// 当只需要查询数量，不需要取出实体时，可用此方法
        /// </summary>
        /// <param name="whereLambda">筛选条件</param>
        /// <returns></returns>
        public static int LoadEntitiesCount<T>(Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                return _db.Queryable<T>().Where(whereLambda).Count();
            }
        }

        /// <summary>
        /// 查找分页之后的实体集合，尚且没实现orderby的Lambda传参，暂时用string代替
        /// </summary>
        /// <typeparam name="s"></typeparam>
        /// <param name="currentIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderLambda"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public static List<T> LoadPageEntities<T>(int currentIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, string order, bool isAsc) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                var temp = _db.Queryable<T>().Where(whereLambda);
                totalCount = temp.Count();
                List<T> model = new List<T>();
                try
                {
                    if (isAsc)
                    {
                        model = temp.OrderBy(order + " asc").Skip((currentIndex - 1) * pageSize).Take(pageSize).ToList();
                    }
                    else
                    {
                        model = temp.OrderBy(order + " desc").Skip((currentIndex - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
                catch
                {
                    throw new Exception("使用指定的order字符串取数据时出错，请查看一下order参数是否正确");
                }
                return model;
            }
        }
        public static List<T> LoadPageEntities<T>(int skip, int take, out int totalCount, string whereString, string order) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                var temp = _db.Queryable<T>().Where(whereString);
                totalCount = temp.Count();
                List<T> model = new List<T>();
                try
                {
                    model = temp.OrderBy(order).Skip(skip).Take(take).ToList();
                }
                catch
                {
                    throw new Exception("使用指定的order字符串取数据时出错，请查看一下order参数是否正确");
                }
                return model;
            }
        }
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static T AddEntity<T>(T entity) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                _db.Insert<T>(entity);
            }
            return entity;
        }
        /// <summary>
        /// 添加实体并返回主键ID
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int AddEntityByIdentity<T>(T entity) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                return _db.Insert<T>(entity, true).ObjToInt();
            }
        }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool UpdateEntity<T>(T entity) where T : class, new()
        {
            using (_db = DbConfig.GetInstance())
            {
                return _db.Update<T>(entity);
            }
        }

    }
}
